/*
 Filename: UDSetDiscount.java
Precondition: manager click discount button.
Post-condition: change discount success.
Main success Scenario:
1. System updates the main UI.
2a. Extensions: not input nombers.
1. error.

 */

package ucm;

/**
 *
 * @author Mark
 */
import model.InputModel;
import model.DatabaseModel;
import view.MgrSetdiscount;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class UCSetDiscount implements UCController {

    private InputModel inputmodel;

    public UCSetDiscount() {
        inputmodel = new InputModel("", "", "", "", "", "", "", "", "");
    }

    public UCSetDiscount(InputModel inputmodel) {
        // System retrieves inputs.
        this.inputmodel = inputmodel;
    }

    public UCSetDiscount(MgrSetdiscount MgrSetdiscount) {
        this.inputmodel = MgrSetdiscount.getinputModel();
    }

    public boolean run() {
        boolean setSuccessful = false;
                if (setMassage()) {
                    JOptionPane.showMessageDialog(new JFrame(),
                    "set Discount Successful!",
                    "Success",
                    JOptionPane.INFORMATION_MESSAGE);
                       setSuccessful = true;
                      new ucm.UCDisplayManagement();
                } else {
                    JOptionPane.showMessageDialog(new JFrame(),
                            "unknow error",
                            "Error Message",
                            JOptionPane.ERROR_MESSAGE);
                    new ucm.UCDisplaySetDiscount();
                }

        return setSuccessful;
    }

    public void run(InputModel inputmodel) {
        this.inputmodel = inputmodel;
        run();
    }


     public boolean setMassage(){
         boolean setOk = false;
        String UserInput = inputmodel.getUserInput();
        String tno = inputmodel.getprice();

        DatabaseModel dbModel = DatabaseModel.getInstance();

        Connection con = dbModel.getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);
             stmt.executeQuery("TRUNCATE TABLE discount");
             stmt.executeUpdate("INSERT INTO discount (`DCOUNT`, `DTICKETNO`)  VALUES ("+UserInput+","+tno+")");
            //database.
         setOk = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return setOk;
    }

}

